﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business
{
    public class PhieuGiaoHang
    {
        
        public int MaPGH { get; set; }
        public string MaPGHHienThi
        {
            get
            {
                return "PGH" + MaPGH;
            }
        }
        public int MaHDB { get; set; }

        public string MaHDBHienThi
        {
            get
            {
                return "HDB" + MaHDB;
            }
        }
        public int MaNV { get; set; }

        public string MaNVHienThi
        {
            get
            {
                return "NV" + MaNV;
            }
        }
        public DateTime NgayGiao { get; set; }
        public string TenKhachHang { get; set; }
        public string DiaChiKhachHang { get; set; }

        public string DienThoai { get; set; }
        public string MaSoThue { get; set; }

        public double TienTruocThue { get; set; }
        public double TienSauThue { get; set; }
        public double ThueSuat { get; set; }
        public double ThueSuatHienThi
        {
            get
            {
                return ThueSuat * 100;
            }
        }

        public List<ChiTietHDBan> DanhSachChiTietMatHang { get; set; }

        public static List<PhieuGiaoHang> LayDanhSach_MaHoaDonBan(string maHDBHienThi)
        {
            try
            {
                int maHDB = Int16.Parse(maHDBHienThi.Substring(3));
                DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
                return (from p in db.PhieuGiaoHangs
                        where p.MaHDB == maHDB
                        select new PhieuGiaoHang(p.MaPGH, p.MaHDB, p.MaNV, p.NgayGiao, p.HoaDonBan.DonDatKH.KhachHang.TenKH, p.HoaDonBan.DonDatKH.KhachHang.DiaChi, p.HoaDonBan.DonDatKH.KhachHang.DienThoai, p.HoaDonBan.DonDatKH.KhachHang.MaSoThue, p.HoaDonBan.TienTruocThue, p.HoaDonBan.TienSauThue, p.HoaDonBan.ThueSuat)).ToList();
            }
            catch
            {
                return LayDanhSach();
            }
        }
       
        public PhieuGiaoHang(int maHDB, string tenKhachHang, string diaChi, string dienthoai, string maSoThue,
            double tienTruocThue, double tienSauThue, double thueSuat)
        {
            this.MaPGH = 0;
            this.MaHDB = maHDB;
            this.TenKhachHang = tenKhachHang;
            this.DiaChiKhachHang = diaChi;
            this.DienThoai = dienthoai;
            this.MaSoThue = maSoThue;

            this.TienTruocThue = tienTruocThue;
            this.TienSauThue = tienSauThue;
            this.ThueSuat = thueSuat;
        }

        public PhieuGiaoHang(int maPGH, int maHDB, int maNV, DateTime ngayGiao,
            string tenKhachHang, string diaChi, string dienthoai, string maSoThue,

            double tienTruocThue, double tienSauThue, double thueSuat)
        {
            this.MaPGH = maPGH;
            this.MaHDB = maHDB;
            this.MaNV = maNV;
            this.NgayGiao = ngayGiao;

            this.TenKhachHang = tenKhachHang;
            this.DiaChiKhachHang = diaChi;
            this.DienThoai = dienthoai;

            this.MaSoThue = maSoThue;
            this.TienTruocThue = tienTruocThue;
            this.TienSauThue = tienSauThue;
            this.ThueSuat = thueSuat;
        }

        
        public static PhieuGiaoHang TaoPhieuGiaoHang(int maHDB)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
            PhieuGiaoHang phieuGiaoHang;
            try
            {
                phieuGiaoHang = (from p in db.PhieuGiaoHangs
                                 where p.MaHDB == maHDB
                                 select new PhieuGiaoHang(p.MaPGH, p.MaHDB, p.MaNV, p.NgayGiao, p.HoaDonBan.DonDatKH.KhachHang.TenKH, p.HoaDonBan.DonDatKH.KhachHang.DiaChi, p.HoaDonBan.DonDatKH.KhachHang.DienThoai, p.HoaDonBan.DonDatKH.KhachHang.MaSoThue, p.HoaDonBan.TienTruocThue, p.HoaDonBan.TienSauThue, p.HoaDonBan.ThueSuat)).Single();
            }
            catch
            {
                phieuGiaoHang = (from p in db.HoaDonBans
                                 where p.MaHDB == maHDB
                                 select new PhieuGiaoHang(p.MaHDB, p.DonDatKH.KhachHang.TenKH, p.DonDatKH.KhachHang.DiaChi, p.DonDatKH.KhachHang.DienThoai, p.DonDatKH.KhachHang.MaSoThue, p.TienTruocThue, p.TienSauThue, p.ThueSuat)).Single();
            }
            List<ChiTietHDBan> danhsachChiTietMatHang = (from p in db.ChiTietHDBans
                                                         where p.MaHDB == maHDB
                                                         select new ChiTietHDBan(p.MaCTHDB, p.MaHDB, p.MaHang, p.HangHoa.TenHang, p.SoLuong, p.DonGiaBan, p.ChietKhau)).ToList();
            if (danhsachChiTietMatHang == null || danhsachChiTietMatHang.Count == 0)
                danhsachChiTietMatHang = new List<ChiTietHDBan>();

            phieuGiaoHang.DanhSachChiTietMatHang = danhsachChiTietMatHang;

            return phieuGiaoHang;
        }

        public static PhieuGiaoHang LayPhieuGiaoHang(int maPGH)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
            PhieuGiaoHang phieuGiaoHang = (from p in db.PhieuGiaoHangs
                                           where p.MaPGH == maPGH
                                           select new PhieuGiaoHang(p.MaPGH, p.MaHDB, p.MaNV, p.NgayGiao, p.HoaDonBan.DonDatKH.KhachHang.TenKH, p.HoaDonBan.DonDatKH.KhachHang.DiaChi, p.HoaDonBan.DonDatKH.KhachHang.DienThoai, p.HoaDonBan.DonDatKH.KhachHang.MaSoThue, p.HoaDonBan.TienTruocThue, p.HoaDonBan.TienSauThue, p.HoaDonBan.ThueSuat)).Single();

            List<ChiTietHDBan> danhsachChiTietMatHang = (from p in db.ChiTietHDBans
                                                         where p.MaHDB == phieuGiaoHang.MaHDB
                                                         select new ChiTietHDBan(p.MaCTHDB, p.MaHDB, p.MaHang, p.HangHoa.TenHang, p.SoLuong, p.DonGiaBan, p.ChietKhau)).ToList();
            if (danhsachChiTietMatHang == null || danhsachChiTietMatHang.Count == 0)
                danhsachChiTietMatHang = new List<ChiTietHDBan>();

            phieuGiaoHang.DanhSachChiTietMatHang = danhsachChiTietMatHang;

            return phieuGiaoHang;
        }

        public static int ThemMoi(PhieuGiaoHang phieuGiaoHang)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            DataAccess.PhieuGiaoHang tamObject = new DataAccess.PhieuGiaoHang();
            tamObject.MaNV = phieuGiaoHang.MaNV;
            tamObject.MaHDB = phieuGiaoHang.MaHDB;
            tamObject.NgayGiao = phieuGiaoHang.NgayGiao;

            db.PhieuGiaoHangs.InsertOnSubmit(tamObject);
            db.SubmitChanges();

            return tamObject.MaPGH;
        }

        public static void CapNhat(PhieuGiaoHang phieuGiaoHang)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            var tamObject = (from p in db.PhieuGiaoHangs
                             where p.MaPGH == phieuGiaoHang.MaPGH
                             select p).Single();

            tamObject.NgayGiao = phieuGiaoHang.NgayGiao;
            db.SubmitChanges();
        }

        public static List<PhieuGiaoHang> LayDanhSach()
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
            return (from p in db.PhieuGiaoHangs
                    select new PhieuGiaoHang(p.MaPGH, p.MaHDB, p.MaNV, p.NgayGiao, p.HoaDonBan.DonDatKH.KhachHang.TenKH, p.HoaDonBan.DonDatKH.KhachHang.DiaChi, p.HoaDonBan.DonDatKH.KhachHang.DienThoai, p.HoaDonBan.DonDatKH.KhachHang.MaSoThue, p.HoaDonBan.TienTruocThue, p.HoaDonBan.TienSauThue, p.HoaDonBan.ThueSuat)).ToList();
        }

        public static List<PhieuGiaoHang> LayDanhSach_NgayGiao(DateTime tungay, DateTime denngay)
        {
            try
            {
                DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
                return (from p in db.PhieuGiaoHangs
                        where p.NgayGiao.Date >= tungay && p.NgayGiao.Date <= denngay
                        select new PhieuGiaoHang(p.MaPGH, p.MaHDB, p.MaNV, p.NgayGiao, p.HoaDonBan.DonDatKH.KhachHang.TenKH, p.HoaDonBan.DonDatKH.KhachHang.DiaChi, p.HoaDonBan.DonDatKH.KhachHang.DienThoai, p.HoaDonBan.DonDatKH.KhachHang.MaSoThue, p.HoaDonBan.TienTruocThue, p.HoaDonBan.TienSauThue, p.HoaDonBan.ThueSuat)).ToList();
            }
            catch
            {
                return LayDanhSach();
            }
        }

        public static List<PhieuGiaoHang> LayDanhSach_MaPhieuGiao(string maPGHHienThi)
        {
            try
            {
                int maPGH = Int16.Parse(maPGHHienThi.Substring(3));
                DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
                return (from p in db.PhieuGiaoHangs
                        where p.MaPGH == maPGH
                        select new PhieuGiaoHang(p.MaPGH, p.MaHDB, p.MaNV, p.NgayGiao, p.HoaDonBan.DonDatKH.KhachHang.TenKH, p.HoaDonBan.DonDatKH.KhachHang.DiaChi, p.HoaDonBan.DonDatKH.KhachHang.DienThoai, p.HoaDonBan.DonDatKH.KhachHang.MaSoThue, p.HoaDonBan.TienTruocThue, p.HoaDonBan.TienSauThue, p.HoaDonBan.ThueSuat)).ToList();
            }
            catch
            {
                return LayDanhSach();
            }
        }

        
     
    }
}
